package edu.gatech.cs2340.group7.wheresmystuffapp.views;

import java.util.List;
import java.util.Locale;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.UiSettings;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import edu.gatech.cs2340.group7.wheresmystuffapp.R;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.ItemLocation;


public class LocationMapView extends FragmentActivity implements OnMapLongClickListener, OnClickListener{
	
	private GoogleMap map;
	private UiSettings mapUiSettings;
	private ItemLocation location;
	private String name, description, category, activity;
	
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_location_map_view);
		displayUseMessage();
		setUp();
		setUpMapIfNeeded();
	}
	
	protected void onResume(){
		super.onResume();
		setUpMapIfNeeded();
	}
	
	private void setUpMapIfNeeded(){
		if(map == null){
			map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.thisIsAMapTest)).getMap();
			if(map != null) {
				setUpMap();
			}
		}
	}
	
	private void setUpMap(){
		map.setMyLocationEnabled(true);
		map.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Hello world"));
		map.setOnMapLongClickListener(this);
		mapUiSettings = map.getUiSettings();
		mapUiSettings.setAllGesturesEnabled(true);
		mapUiSettings.setMyLocationButtonEnabled(true);
	}
	
	private void displayUseMessage(){
		AlertDialog.Builder d = new AlertDialog.Builder(this, AlertDialog.THEME_TRADITIONAL);
		d.setMessage("To Select a location, tap and hold your finger over the spot you want");
		d.setTitle("How To select your location");
		d.setPositiveButton("Got it", new OnClickListener(){

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();				
			}
			
		});
		d.show();
	}

	@Override
	public void onMapLongClick(LatLng point) {
		double latitude = point.latitude;
		double longitude = point.longitude;
		location = new ItemLocation(latitude, longitude);
		AlertDialog.Builder d = new AlertDialog.Builder(this, AlertDialog.THEME_TRADITIONAL);
		d.setMessage("You selected " + getAddress() + ". Is this correct?");
		d.setPositiveButton("Yes. It is", this);
		d.setNegativeButton("No, it isn't", new OnClickListener(){

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				
			}
			
		});
		d.show();
		//Toast.makeText(this, "latitude: " + latitude + ", longitude: " + longitude, Toast.LENGTH_SHORT).show();
		//map.addMarker(new MarkerOptions().position(point).title("Item Location"));
	}
	
	private String getAddress(){
		Geocoder coder = new Geocoder(getBaseContext(), Locale.getDefault());
		List<Address> address = null;
		String answer;
		System.out.println("Attempting to get location");
		try{
			address = coder.getFromLocation(location.getLatitude() / 1E6, location.getLongitude() / 1E6, 5);
			System.out.println("Address is" + address.get(0).toString());
			answer = address.get(0).getLocality() + ", " + address.get(0).getCountryName();
			System.out.println("Got Address");
		} catch(Exception e){
			e.printStackTrace();
			System.out.println("Could not get location");
			answer = "could not get address";
		}
		
		return answer;
	}
	
	private void setUp(){
		Intent intent = getIntent();
		name = intent.getStringExtra("Name");
		description = intent.getStringExtra("Description");
		category = intent.getStringExtra("Category");
		activity = intent.getStringExtra("Activity");
		Toast.makeText(this, "Item name: " + name + ", Item des: " + description + ", Item cat: " + category + ", activity: " + activity, Toast.LENGTH_LONG).show();
	}

	@Override
	public void onClick(DialogInterface dialog, int which) {
		returnToActivity();
	}
	
	private void returnToActivity(){
		Intent intent;
		//if(activity.equalsIgnoreCase("Lost")){
			intent = new Intent(this, ReportLostItemView.class);
//		}
//		if(activity.equalsIgnoreCase("Found")){
//			intent = new Intent(this, ReportFoundItemView.class);
//		}
//		else{
//			intent = new Intent(this, ReportDonationItemView.class);
//		}
		
		intent.putExtra("Name", name);
		intent.putExtra("Description", description);
		intent.putExtra("Category", category);
		intent.putExtra("Item Latitude", location.getLatitude());
		intent.putExtra("Item Longitude", location.getLongitude());
		startActivity(intent);
	}
}
